<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The DateTime class</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="datetime.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.add.html">DateTime::add</a></div>
 <div class="up"><a href="book.datetime.html">Date/Time</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="class.datetime" class="reference">

 <h1 class="title">The DateTime class</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.2.0)</p>


  <div class="section" id="datetime.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    Representation of date and time.
   </p>
  </div>


  <div class="section" id="datetime.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <strong class="classname">DateTime</strong>
     </span>
     <span class="oointerface">implements 
      <span class="interfacename"><a href="class.datetimeinterface.html" class="interfacename">DateTimeInterface</a></span>
     </span>
     {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 常量 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.atom"><var class="varname">ATOM</var></a></var>
     <span class="initializer"> = &quot;Y-m-d\TH:i:sP&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.cookie"><var class="varname">COOKIE</var></a></var>
     <span class="initializer"> = &quot;l, d-M-y H:i:s T&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.iso8601"><var class="varname">ISO8601</var></a></var>
     <span class="initializer"> = &quot;Y-m-d\TH:i:sO&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc822"><var class="varname">RFC822</var></a></var>
     <span class="initializer"> = &quot;D, d M y H:i:s O&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc850"><var class="varname">RFC850</var></a></var>
     <span class="initializer"> = &quot;l, d-M-y H:i:s T&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc1036"><var class="varname">RFC1036</var></a></var>
     <span class="initializer"> = &quot;D, d M y H:i:s O&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc1123"><var class="varname">RFC1123</var></a></var>
     <span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc2822"><var class="varname">RFC2822</var></a></var>
     <span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rfc3339"><var class="varname">RFC3339</var></a></var>
     <span class="initializer"> = &quot;Y-m-d\TH:i:sP&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.rss"><var class="varname">RSS</var></a></var>
     <span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>
    ;</div>

    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetime.html#datetime.constants.w3c"><var class="varname">W3C</var></a></var>
     <span class="initializer"> = &quot;Y-m-d\TH:i:sP&quot;</span>
    ;</div>

    
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="datetime.construct.html" class="methodname">__construct</a></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code><span class="initializer"> = &quot;now&quot;</span></span>
   [, <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ]] )</div>

    
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.add.html" class="methodname">add</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <code class="parameter">$interval</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.createfromformat.html" class="methodname">createFromFormat</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code></span>
   [, <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span><span class="methodname"><a href="datetime.getlasterrors.html" class="methodname">getLastErrors</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.modify.html" class="methodname">modify</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$modify</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.set-state.html" class="methodname">__set_state</a></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.setdate.html" class="methodname">setDate</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$year</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$month</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$day</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.setisodate.html" class="methodname">setISODate</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$year</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$week</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$day</code><span class="initializer"> = 1</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.settime.html" class="methodname">setTime</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$hour</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$minute</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$second</code><span class="initializer"> = 0</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.settimestamp.html" class="methodname">setTimestamp</a></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$unixtimestamp</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.settimezone.html" class="methodname">setTimezone</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTime</span><span class="methodname"><a href="datetime.sub.html" class="methodname">sub</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span> <code class="parameter">$interval</code></span>
   )</div>

    
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateInterval</span><span class="methodname"><a href="datetime.diff.html" class="methodname">diff</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$datetime2</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = false</span></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">string</span><span class="methodname"><a href="datetime.format.html" class="methodname">format</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="datetime.getoffset.html" class="methodname">getOffset</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">int</span><span class="methodname"><a href="datetime.gettimestamp.html" class="methodname">getTimestamp</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">DateTimeZone</span><span class="methodname"><a href="datetime.gettimezone.html" class="methodname">getTimezone</a></span>
    ( <span class="methodparam">void</span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="datetime.wakeup.html" class="methodname">__wakeup</a></span>
    ( <span class="methodparam">void</span>
   )</div>


   }</div>


  </div>
  

  <div class="section" id="datetime.constants.types">
   <h2 class="title">预定义常量</h2>
   <dl>

    
     <dt id="datetime.constants.atom"><strong><code>DateTime::ATOM</code></strong></dt>

     <dt>
<strong><code>DATE_ATOM</code></strong></dt>

     <dd>

      <span class="simpara">
       Atom (example: 2005-08-15T15:52:01+00:00)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.cookie"><strong><code>DateTime::COOKIE</code></strong></dt>

     <dt>
<strong><code>DATE_COOKIE</code></strong></dt>

     <dd>

      <span class="simpara">
       HTTP Cookies (example: Monday, 15-Aug-05 15:52:01 UTC)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.iso8601"><strong><code>DateTime::ISO8601</code></strong></dt>

     <dt>
<strong><code>DATE_ISO8601</code></strong></dt>

     <dd>

      <span class="simpara">
       ISO-8601 (example: 2005-08-15T15:52:01+0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc822"><strong><code>DateTime::RFC822</code></strong></dt>

     <dt>
<strong><code>DATE_RFC822</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 822 (example: Mon, 15 Aug 05 15:52:01 +0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc850"><strong><code>DateTime::RFC850</code></strong></dt>

     <dt>
<strong><code>DATE_RFC850</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 850 (example: Monday, 15-Aug-05 15:52:01 UTC)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc1036"><strong><code>DateTime::RFC1036</code></strong></dt>

     <dt>
<strong><code>DATE_RFC1036</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 1036 (example: Mon, 15 Aug 05 15:52:01 +0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc1123"><strong><code>DateTime::RFC1123</code></strong></dt>

     <dt>
<strong><code>DATE_RFC1123</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 1123 (example: Mon, 15 Aug 2005 15:52:01 +0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc2822"><strong><code>DateTime::RFC2822</code></strong></dt>

     <dt>
<strong><code>DATE_RFC2822</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 2822 (Mon, 15 Aug 2005 15:52:01 +0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rfc3339"><strong><code>DateTime::RFC3339</code></strong></dt>

     <dt>
<strong><code>DATE_RFC3339</code></strong></dt>

     <dd>

      <span class="simpara">
       Same as <strong><code>DATE_ATOM</code></strong> (since PHP 5.1.3)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.rss"><strong><code>DateTime::RSS</code></strong></dt>

     <dt>
<strong><code>DATE_RSS</code></strong></dt>

     <dd>

      <span class="simpara">
       RSS (Mon, 15 Aug 2005 15:52:01 +0000)
      </span>
     </dd>

    

    
     <dt id="datetime.constants.w3c"><strong><code>DateTime::W3C</code></strong></dt>

     <dt>
<strong><code>DATE_W3C</code></strong></dt>

     <dd>

      <span class="simpara">
       World Wide Web Consortium (example: 2005-08-15T15:52:01+00:00)
      </span>
     </dd>

    

   </dl>

  </div>



 <div class="section" id="datetime.changelog">
  <h2 class="title">更新日志</h2>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.0</td>
       <td>
        The class now implements <a href="class.datetimeinterface.html" class="classname">DateTimeInterface</a>.
       </td>
      </tr>

      <tr>
       <td>5.2.2</td>
       <td>
        DateTime object comparison with the
        <a href="language.operators.comparison.html" class="link">comparison operators</a>
        changed to work as expected. Previously, all DateTime objects were
        considered equal (using <em>==</em>).
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 </div>

 





















































































<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="datetime.add.html">DateTime::add</a> — Adds an amount of days, months, years, hours, minutes and seconds to a
   DateTime object</li><li><a href="datetime.construct.html">DateTime::__construct</a> — Returns new DateTime object</li><li><a href="datetime.createfromformat.html">DateTime::createFromFormat</a> — Returns new DateTime object formatted according to the specified format</li><li><a href="datetime.getlasterrors.html">DateTime::getLastErrors</a> — Returns the warnings and errors</li><li><a href="datetime.modify.html">DateTime::modify</a> — Alters the timestamp</li><li><a href="datetime.set-state.html">DateTime::__set_state</a> — The __set_state handler</li><li><a href="datetime.setdate.html">DateTime::setDate</a> — Sets the date</li><li><a href="datetime.setisodate.html">DateTime::setISODate</a> — Sets the ISO date</li><li><a href="datetime.settime.html">DateTime::setTime</a> — Sets the time</li><li><a href="datetime.settimestamp.html">DateTime::setTimestamp</a> — Sets the date and time based on an Unix timestamp</li><li><a href="datetime.settimezone.html">DateTime::setTimezone</a> — Sets the time zone for the DateTime object</li><li><a href="datetime.sub.html">DateTime::sub</a> — Subtracts an amount of days, months, years, hours, minutes and seconds from
   a DateTime object</li></ul>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="datetime.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.add.html">DateTime::add</a></div>
 <div class="up"><a href="book.datetime.html">Date/Time</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
